/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-29
 * @Time  : 下午3:47
 * @brief :
 */
var createUi = require('wap_common:widget/ui/base/base.js').createUi;

exports.Login = createUi({
    _data:{
        loginUrl:'/wap/wallet',
        close:false
    },

    _create:function(){
        var gid = this.getGid(),
            tpl = __inline('./login.tmpl');

        var  html = tpl({gid:gid,close:this.data('close')});
        $('body').append(html);

        var $root = $('#'+gid),
            $container = $root.find('.dialog-container'),
            $userName= $container.find('.user-name'),
            $password = $container.find('.password'),
            $btnLogin = $container.find('.btn-login');

        this.data({
            '$root':$root,
            '$container':$container,
            '$userName':$userName,
            '$password':$password,
            '$btnLogin':$btnLogin
        });

        if(localStorage.getItem("zhengrongbao_user_name")){
            $userName.val(localStorage.getItem("zhengrongbao_user_name"))
        }

    },

    _init:function(){
        var me = this,
            $loginBtn = this.data('$btnLogin');

        $loginBtn.on('touchstart',function(){
            console.log(me.data('$root'));
            var _me = this;
            if(!$(this).hasClass('btn-disable')){
                if(me._check()){
                    var $username = me.data('$userName'),
                        $password = me.data('$password');
                    $(_me).addClass('btn-disable');
                    $.post('/account/loginuser',{
                        'user_name': $.trim($username.val()),
                        'passwd': $.trim($password.val())
                    }, function(data) {
                        $(_me).removeClass('btn-disable');
                        if(data && data.error_no == 0) {
                            location.href = me.data('loginUrl');
                            localStorage.setItem("zhengrongbao_user_name",$.trim($username.val()))
                        } else {
                            me._showTip(data.error_message);
                        }
                    },'json');
                }
            }
        });

        this.data("$container").find('.close-btn').on('touchstart',function(){
            me.hide();
        });
    },



    _check:function(){
        var me = this,
            $userName = this.data('$userName'),
            $password = this.data('$password');

        this._hideTip();
        if(!/^\d{11}$/.test($.trim($userName.val()))){
            me._showTip('手机号输入错误');
            return false;
        }

        if($.trim($password.val()).length < 6 || $.trim($password.val()).length > 16){
            me._showTip('密码输入错误');
            return false;
        }
        return true;
    },

    _hideTip:function(){
        this.data('$container').find('.tip').css("visibility","hidden");
    },

    _showTip:function(text){
        this.data('$container').find('.tip').css("visibility","visible").text(text);
    },

    show:function(){
        this.data('$root').show();
    },

    hide:function(){
        this.data('$root').hide();
    }
});